﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BaseListControl.ascx.cs" Inherits="WebUI.Controls.BaseListControl" %>

<script type="text/javascript">
    var <%=ClientID %>clickOnCheckbox;
    var <%=ClientID %>selectedRow = null;       
    var <%=ClientID %>selectedCheckbox = null; 
    var <%=ClientID %>selectedID = null; 
    var <%=ClientID %>selectedClass = null; 
    
    function resetGridSelection<%=ClientID %>(){
        try{
            <%=ClientID %>selectedRow = null; 
            if(<%=ClientID %>selectedCheckbox != null)
                <%=ClientID %>selectedCheckbox.checked = false;      
            <%=ClientID %>selectedCheckbox = null; 
            <%=ClientID %>selectedID = null; 
            <%=ClientID %>selectedClass = null; 
        }
        catch(ex){
        }
    }
    
    function clickOnCheckBox<%=ClientID %>(cb){
        <%=ClientID %>clickOnCheckbox = true;
    }
    
    function onItemClick<%=ClientID %>(sender)
    {
        if(<%=ClientID %>clickOnCheckbox){
            <%=ClientID %>clickOnCheckbox = false;
            return;
        }
        
        if(<%=ClientID %>selectedRow != null){
            if(<%=ClientID %>selectedRow.className)
                <%=ClientID %>selectedRow.className = <%=ClientID %>selectedClass;
            else
                <%=ClientID %>selectedRow.setAttribute("class", <%=ClientID %>selectedClass);
            if(<%=ClientID %>selectedCheckbox != null)
                <%=ClientID %>selectedCheckbox.checked = false;
        }
        
        if(sender.className){
            <%=ClientID %>selectedClass = sender.className;
            sender.className = "CustomGridSelectedItem";
        }
        else{
            <%=ClientID %>selectedClass = sender.getAttribute("class");
            sender.setAttribute("class", "CustomGridSelectedItem");
        }
        <%=ClientID %>selectedRow = sender;
        
        var inputs = sender.getElementsByTagName("input");
        for(var i = 0; i < inputs.length; i++){
            if(inputs[i].type == "checkbox" && inputs[i].id.indexOf("cbSelect") >= 0){
                <%=ClientID %>selectedCheckbox = inputs[i];
                inputs[i].checked = true;
                break;
            }
        }

        document.getElementById("<%= hidSelectedID.ClientID %>").value = <%=ClientID %>selectedRow.getAttribute("ItemID");
    }
    
    function selectAll<%=ClientID %>(cbAll){
        var grid = document.getElementById("<%=grid.ClientID %>");
        var inputs = grid.getElementsByTagName("input");
        for(var i = 0; i < inputs.length; i++){
            if(inputs[i].type == "checkbox"){
                inputs[i].checked = cbAll.checked;
            }
        }
    }
    
    function getSelectedItemID<%=ClientID %>()
    {
        return document.getElementById("<%= hidSelectedID.ClientID %>").value;
    }
</script>

<asp:DataGrid ID="grid" runat="server" AllowPaging="true" AllowSorting="true" GridLines="None" CssClass="customGrid"
    AutoGenerateColumns="false" EmptyDataText="No record to display" ItemStyle-Wrap="true" CellPadding="1" CellSpacing="1"
    OnSortCommand="grid_OnSortCommand" OnItemCreated="grid_OnItemCreated" OnItemDataBound="grid_OnItemDataBound"
    OnPageIndexChanged="grid_OnPageIndexChanged" AllowCustomPaging="true" UseAccessibleHeader="true">
    <PagerStyle Mode="NumericPages" PageButtonCount="10" />
    <Columns>
        <asp:TemplateColumn ItemStyle-Width="50px" HeaderStyle-Width="50px" HeaderStyle-HorizontalAlign="Left">
            <HeaderTemplate><asp:CheckBox ID="cbSelectAll" runat="server" /></HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="cbSelect" runat="server" ItemID='<%# Eval("ID") %>' />
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:BoundColumn HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="50px" DataField="ID" SortExpression="ID" HeaderText="ID"></asp:BoundColumn>
        <asp:BoundColumn HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="350px" DataField="Name" SortExpression="Name" HeaderText="Name"></asp:BoundColumn>
        <asp:BoundColumn HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="150px" DataField="Objective" SortExpression="Objective" HeaderText="Objective"></asp:BoundColumn>
        <asp:BoundColumn HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="150px" DataField="TypeName" SortExpression="TypeName" HeaderText="TypeName"></asp:BoundColumn>
    </Columns>
</asp:DataGrid>
<asp:HiddenField ID="hidSelectedID" runat="server" />